<?php
global $wpdb;
global $table_rounds,$table_shooters,$table_events,$table_eventtypes;
if($_REQUEST['action'] == 'addscore')
{
	$r1 = 0; $r2 = 0; $r3 = 0; $r4 = 0;
	for($ri = 101; $ri <= 125; $ri++)
		$r1 += $_REQUEST[$ri];
	for($ri = 201; $ri <= 225; $ri++)
		$r2 += $_REQUEST[$ri];
	for($ri = 301; $ri <= 325; $ri++)
		$r3 += $_REQUEST[$ri];
	for($ri = 401; $ri <= 425; $ri++)
		$r4 += $_REQUEST[$ri];

	$query = $wpdb->prepare("insert into $table_rounds (eventid,shooterid,round1,round2,round3,round4) values (%d, %d, %d, %d, %d, %d)",
							$_REQUEST['events_eventid'],
							$_REQUEST['shooters_shooterid'],
							$r1,
							$r2,
							$r3,
							$r4);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'changescore')
{
	$r1 = 0; $r2 = 0; $r3 = 0; $r4 = 0;
	for($ri = 101; $ri <= 125; $ri++)
		$r1 += $_REQUEST[$ri];
	for($ri = 201; $ri <= 225; $ri++)
		$r2 += $_REQUEST[$ri];
	for($ri = 301; $ri <= 325; $ri++)
		$r3 += $_REQUEST[$ri];
	for($ri = 401; $ri <= 425; $ri++)
		$r4 += $_REQUEST[$ri];
		
	$query = $wpdb->prepare("update $table_rounds set eventid=%d, shooterid=%d, round1=%d, round2=%d, round3=%d, round4=%d where $table_rounds.roundid=%d",
							$_REQUEST['events_eventid'],
							$_REQUEST['shooters_shooterid'],
							$r1,
							$r2,
							$r3,
							$r4,
							$_REQUEST['rounds_roundid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}    
}
else if($_REQUEST['action'] == 'remove')
{
	$query = $wpdb->prepare("delete from $table_rounds where $table_rounds.roundid=%d", $_REQUEST['rounds_roundid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'edit')
{
	$query = $wpdb->prepare("select $table_rounds.roundid, $table_rounds.eventid, $table_rounds.shooterid, $table_rounds.round1, $table_rounds.round2, $table_rounds.round3, $table_rounds.round4 from $table_rounds where $table_rounds.roundid=%s",
								$_REQUEST['rounds_roundid']);
	$rounddata = $wpdb->get_row($query, ARRAY_A);
	if($rounddata === FALSE)
	{
		die($wpdb->print_error());
	}
	$shooters = $wpdb->get_results("select $table_shooters.shooterid, $table_shooters.firstname, $table_shooters.lastname from $table_shooters order by lastname, firstname");
	if($shooters === FALSE)
	{
		die($wpdb->print_error());
	}
	$events = $wpdb->get_results("select $table_events.eventid, $table_shoots.name as shootname, $table_eventtypes.name as eventtypename from $table_events join $table_shoots on $table_events.shootid = $table_shoots.shootid join $table_eventtypes on $table_events.eventtypeid = $table_eventtypes.eventtypeid order by shootname");
	if($events === FALSE)
	{
		die($wpdb->print_error());
	}

?>
<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Edit Score:</h2>
<div>
<label for="events">Shoot, Event</label>
<select name="events_eventid" id="events">
<?php
foreach($events as $event)
{
	if($event->eventid == $rounddata['eventid'])
		echo "<option selected value=" . $event->eventid . ">" . $event->shootname . ", " . $event->eventtypename . "</option>\n";
	else
		echo "<option value=" . $event->eventid . ">" . $event->shootname . ", " . $event->eventtypename . "</option>\n";
}
?>
</select><br />
<label for="shooter">Shooter:</label>
<select name="shooters_shooterid" id="shooter">
<?php
foreach($shooters as $shooter)
{
	if($shooter->shooterid == $rounddata['shooterid'])
		echo "<option selected value=" . $shooter->shooterid . ">" . $shooter->lastname . "," .  $shooter->firstname . "</option>\n";
	else
		echo "<option value=" . $shooter->shooterid . ">" . $shooter->lastname . "," .  $shooter->firstname . "</option>\n";
}
?>
</select><br />
<?php
	for($ri=100;$ri<=400;$ri += 100)
	{
		$round = $ri/100;
		$roundvalue = $rounddata['round' .$round];
		echo "<table border=1>\n";
		echo "<tr><th colspan=26>Round $round</th></tr>\n";
		echo "<tr><td colspan=2>1</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>2</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>3</td>" .
			     "<td colspan=2>4</td>" .
			     "<td colspan=2>5</td>" .
			     "<td colspan=2>6</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>7</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>8</td>";
		echo "<tr><td>H</td><td>L</td><td>H</td><td>L</td>" . 
		         "<td>H</td><td>L</td><td>H</td><td>L</td>" . 
		         "<td>H</td><td>L</td>" .
		         "<td>H</td><td>L</td>" . 
		         "<td>H</td><td>L</td>" .
		         "<td>H</td><td>L</td><td>L</td><td>H</td>" . 
		         "<td>H</td><td>L</td><td>L</td><td>H</td>" .
		         "<td>H</td><td>L</td>" . 
		         "<td>Opt</td>";	
		echo "<tr>\n";
		for($ti=1; $ti<=25; $ti++)
		{
			$target = $ri + $ti;
			$value = 1 << (25-$ti);
			if(($roundvalue & $value) > 0)
				echo "<td><input type=checkbox name='" . $target . "' value=" . $value . " checked /></td>\n";
			else
				echo "<td><input type=checkbox name='" . $target . "' value=" . $value . " /></td>\n";
		}
		echo "</tr>";
		echo "</table>\n";
	}
?>
<input type="hidden" name="rounds_roundid" value="<?php echo $rounddata['roundid'] ?>" />
<input type="hidden" name="action" value="changescore" />
</div>
<div class="submit"><input type="submit" value="Update Score &raquo;" /></div>
</div>
</form>
<?php
return;
}
?>

<form action="admin.php?page=<?php print $GLOBALS['ts_path']; ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Current Scores</h2>
<table>
<th width=15%>Shoot</th><th width=10%>Event</th><th width=15%>Name</th><th width=5%>Round1</th><th width=5%>Round2</th><th width=5%>Round3</th><th width=5%>Round4</th><th width=5%>Total</th><th colspan="3">Actions</th>
<?php
$rounds = $wpdb->get_results("SELECT $table_rounds.roundid, $table_shoots.name as shootname, $table_eventtypes.name as eventname, $table_shooters.firstname, $table_shooters.lastname, BIT_COUNT($table_rounds.Round1) as r1, BIT_COUNT($table_rounds.Round2) as r2, BIT_COUNT($table_rounds.Round3) as r3, BIT_COUNT($table_rounds.Round4) as r4, BIT_COUNT($table_rounds.Round1) + BIT_COUNT($table_rounds.Round2) + BIT_COUNT($table_rounds.Round3) + BIT_COUNT($table_rounds.Round4) as rt " .
					  "FROM $table_rounds " . 
				      "join $table_shooters on $table_rounds.shooterid = $table_shooters.shooterid " .
					  "join $table_events on $table_rounds.eventid = $table_events.eventid " .
					  "join $table_eventtypes on $table_events.eventtypeid=$table_eventtypes.eventtypeid " .
					  "join $table_shoots on $table_events.shootid = $table_shoots.shootid " .
					  "order by $table_shoots.date asc", ARRAY_A);
if ($rounds === FALSE) {
    die($wpdb->print_error());
}
foreach($rounds as $round)
{
    echo "<tr>";
    echo "<td>" . $round['shootname'] . "</td>\n";
    echo "<td>" . $round['eventname'] . "</td>\n";
    $total = 0;
    echo "<td>" . $round['lastname'] . ", " . $round['firstname'] . "</td>\n";
    for($i = 1; $i<5; $i++)
    {
    	$total += $round["r$i"];
    	echo "<td>" . $round["r$i"] . "</td>\n";
    }
    echo "<td>" . $round['rt'] . "</td>\n";
    echo "<td>" . $round['rangename'] . "</td>\n";
    echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;rounds_roundid=" . $round['roundid'] . "&amp;action=edit\" class=\"edit\">Edit</a></td>";
    echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;rounds_roundid=" . $round['roundid'] . "&amp;action=remove\" class=\"delete\">Remove</a></td>";
    echo "<td><input type=\"checkbox\" name=\"rounds_roundid[]\" value=\"" . $round['roundid']. "\"></td></tr>\n";
}
?>
</table>
<div class="submit">
<input type="submit" class="delete" name="action" value="Remove Selected Scores" />
</div>
</div>
</form>

<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Add a New Score:</h2>
<div>
<?php
$shooters = $wpdb->get_results("select $table_shooters.shooterid, $table_shooters.firstname, $table_shooters.lastname from $table_shooters order by lastname, firstname");
if($shooters === FALSE)
{
	die($wpdb->print_error());
}
$events = $wpdb->get_results("select $table_events.eventid, $table_shoots.name as shootname, $table_eventtypes.name as typename from $table_events join $table_shoots on $table_events.shootid = $table_shoots.shootid join $table_eventtypes on $table_events.eventtypeid = $table_eventtypes.eventtypeid order by shootname");
if($events === FALSE)
{
	die($wpdb->print_error());
}
?>
<label for="events">Shoot, Event</label>
<select name="events_eventid" id="events">
<?php
    foreach($events as $event)
    {
        echo "<option value=" . $event->eventid . ">" . $event->shootname . ", " . $event->eventtypename . "</option>\n";
    }
?>
</select><br />
<label for="shooter">Shooter:</label>
<select name="shooters_shooterid" id="shooter">
<?php
	foreach($shooters as $shooter)
	{
		echo "<option value=" . $shooter->shooterid . ">" . $shooter->lastname . "," .  $shooter->firstname . "</option>\n";
	}
?>
</select><br />
<?php
	for($ri=100;$ri<=400;$ri += 100)
	{
		$round = $ri/100;
		echo "<table border=1>\n";
		echo "<tr><th colspan=26>Round $round</th></tr>\n";
		echo "<tr><td colspan=2>1</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>2</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>3</td>" .
			     "<td colspan=2>4</td>" .
			     "<td colspan=2>5</td>" .
			     "<td colspan=2>6</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>7</td><td colspan=2>DBLS</td>" .
			     "<td colspan=2>8</td>";
		echo "<tr><td>H</td><td>L</td><td>H</td><td>L</td>" . 
		         "<td>H</td><td>L</td><td>H</td><td>L</td>" . 
		         "<td>H</td><td>L</td>" .
		         "<td>H</td><td>L</td>" . 
		         "<td>H</td><td>L</td>" .
		         "<td>H</td><td>L</td><td>L</td><td>H</td>" . 
		         "<td>H</td><td>L</td><td>L</td><td>H</td>" .
		         "<td>H</td><td>L</td>" . 
		         "<td>Opt</td>";	
		echo "<tr>\n";
		for($ti=1; $ti<=25; $ti++)
		{
			$target = $ri + $ti;
			$value = 1 << (25-$ti);
			echo "<td><input type=checkbox name='" . $target . "' value=" . $value . " checked /></td>\n";
		}
		echo "</tr>";
		echo "</table>\n";
	}
?>
<input type="hidden" name="action" value="addscore" />
</div>
<div class="submit"><input type="submit" value="Add Score &raquo;" /></div>
</div>
</form>
